package com.videomeetting.legend.videomeetting.activity.adapater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;


import com.videomeetting.legend.videomeetting.R;
import com.videomeetting.legend.videomeetting.activity.bean.VideoBean;
import com.videomeetting.legend.videomeetting.activity.fragment.ChooseAllCallback;
import com.videomeetting.legend.videomeetting.activity.view.BorderCheckBox;
import com.videomeetting.legend.videomeetting.activity.view.BorderTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2018/4/11.
 */

public class FileListAdapater extends RecyclerView.Adapter<FileListAdapater.ViewHolder> {
    private Context mContext;
    List<VideoBean> videoBeans;
    List<VideoBean> chooseList;
    private  HashMap<Integer, Boolean> map;
    //这个是checkbox的Hashmap集合
    private ChooseAllCallback callback;

    public ChooseAllCallback getCallback() {
        return callback;
    }

    public void setCallback(ChooseAllCallback callback) {
        this.callback = callback;
    }



    public List<VideoBean> getChooseList() {
        return chooseList;
    }

    public void setChooseList(List<VideoBean> chooseList) {
        this.chooseList = chooseList;
    }

    public FileListAdapater(Context context, List<VideoBean> videoBeans) {
        this.mContext = context;
        this.videoBeans = videoBeans;
        map = new HashMap<>();
        chooseList = new ArrayList<>();
        for (int i = 0; i < videoBeans.size(); i++) {
            //添加30条数据
            map.put(i, false);
        }
    }
    /**
     * 全选
     */
    public void all() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        boolean shouldall = false;
        for (Map.Entry<Integer, Boolean> entry : entries) {
            Boolean value = entry.getValue();
            if (!value) {
                shouldall = true;
                break;
            }
        }
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(shouldall);
        }
        notifyDataSetChanged();
    }

    /**
     * 反选
     */
    public void neverall() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(!entry.getValue());
        }
        notifyDataSetChanged();
    }
    public List<VideoBean> getVideoBeans() {
        return videoBeans;
    }

    public void setVideoBeans(List<VideoBean> videoBeans) {
        this.videoBeans = videoBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.file_item, null, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (videoBeans!=null&&videoBeans.size() >= 1) {
            if (position == 0) {
                holder.fileName.setText(videoBeans.get(position).getName());
                holder.fileTime.setText(videoBeans.get(position).getTime());
                holder.fileLenght.setText(videoBeans.get(position).getLenght());
                holder.fileAllChoose.setVisibility(View.VISIBLE);
            } else {
                holder.fileName.setText(videoBeans.get(position).getName());
                holder.fileTime.setText(videoBeans.get(position).getTime());
                holder.fileLenght.setText(videoBeans.get(position).getLenght());
                holder.fileAllChoose.setVisibility(View.INVISIBLE);
            }
        }
        holder.fileChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.fileChoose.isChecked()){
                    chooseList.add(videoBeans.get(position));
                }else {
                    for (int i = 0; i < chooseList.size(); i++) {
                        if(chooseList.contains(videoBeans.get(position))){
                            chooseList.remove(videoBeans.get(position));
                        }
                    }
                }
            }
        });
        holder.fileAllChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.fileAllChoose.isChecked()) {
                    all();
                }else {
                    neverall();

                }
            }
        });
        holder.fileChoose.setChecked(map.get(position));
    }

    @Override
    public int getItemCount() {
        if (videoBeans!=null){
            return videoBeans.size();
        }else {
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        BorderTextView fileName;
        BorderTextView fileTime;
        BorderTextView fileLenght;
        BorderCheckBox fileChoose;
        BorderCheckBox fileAllChoose;

        public ViewHolder(View itemView) {
            super(itemView);
            fileName = (BorderTextView) itemView.findViewById(R.id.file_item_file_name);
            fileTime = (BorderTextView) itemView.findViewById(R.id.file_item_file_time);
            fileLenght = (BorderTextView) itemView.findViewById(R.id.file_item_file_lenght);
            fileChoose = (BorderCheckBox) itemView.findViewById(R.id.file_item_choose);
            fileAllChoose = (BorderCheckBox) itemView.findViewById(R.id.file_item_all_choose);
        }
    }

}